package com.clstu.jdbcs;

import com.mysql.jdbc.Driver;
import org.testng.annotations.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * 这是一个探讨jdbc的5种连接方式的类
 * 传统方式得到驱动driver对象就能得到连接!!!!!!
 * 升级版本就只需要DriverManager(帮我们做了很多工做)就可以得到连接了!!!!!(最常使用)
 * 最终版本是用到是连接池技术(自己改好配置文件)!!!!!
 */
public class JdbcConnect {
    public static void main(String[] args) {

    }

    @Test//方式1(原始版本)
    public void method1() throws SQLException {
        //1.注册驱动
        Driver driver = new Driver();//静态加载Driver类
        //2.得到连接
        String url = "jdbc:mysql://localhost:3306/cl_db01";//jdbc的协议规定就是样写的
        Properties properties = new Properties();
        properties.setProperty("user","root");
        properties.setProperty("password","root");
        Connection connect = driver.connect(url, properties);//得到网络连接,类似数据通路
        connect.close();
    }
    @Test//方式2
    public void method2() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
        //利用反射得到Driver类
        //使用反射机制得到Driver的class对象(加载类),更加的灵活
        Class<?> cls = Class.forName("com.mysql.jdbc.Driver");
        Driver driver = (Driver) cls.newInstance();//通过class对象得到实例

        String url = "jdbc:mysql://localhost:3306/cl_db01";
        Properties properties = new Properties();//根据规定,需要Properties对象传入得到连接
        properties.setProperty("user","root");//用户名
        properties.setProperty("password","root");//密码
        Connection connect = driver.connect(url, properties);
        System.out.println("方式2   "+connect);
    }
    @Test //方式3
    public void method3() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
        Class<?> cls = Class.forName("com.mysql.jdbc.Driver");//利用反射机制加载类
        Driver driver =(Driver) cls.newInstance();
        //写好 user,password,url
        String url = "jdbc:mysql://localhost:3306/cl_db01";
        String user="root";
        String password="root";
        //注册Driver驱动后,用DriverManager得到连接(不需要properties对象了)
        DriverManager.registerDriver(driver);//注册Driver驱动
        Connection connection = DriverManager.getConnection(url, user, password);//得到连接
        System.out.println("方式3==="+connection);
    }
    @Test//方式4
       // (比方式3少了一条注册驱动语句,系统帮我们完成了) 更简单
    /*   //自动注册驱动的源码(Driver的静态代码块)
        static {
            try {
                DriverManager.registerDriver(new Driver());//这里,类加载的时候就已经完成了注册驱动
            } catch (SQLException var1) {
                throw new RuntimeException("Can't register driver!");
            }
        }
     */
    public void method4() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
        //甚至这句话也可以不写,系统已经帮我们加载号Driver对象了(在jdbc高版本的配置文件里)
        Class<?> cls = Class.forName("com.mysql.jdbc.Driver");//利用反射机制加载类(之后不需要Driver对象来驱动)
        //写好 user,password,url
        String url = "jdbc:mysql://localhost:3306/cl_db01";
        String user="root";
        String password="root";
        //不需要注册Driver驱动(系统帮我们驱动了),用DriverManager得到连接(不需要properties对象了)
        Connection connection = DriverManager.getConnection(url, user, password);//得到连接
        System.out.println("方式4==="+connection);
    }
    @Test//方式5
    // 在方式4的基础上利用配置文件获取连接(更加灵活)
    public void method5() throws IOException, ClassNotFoundException, SQLException {
        Properties properties = new Properties();//通过properties类读取properties配置文件的信息连接
        properties.load(new FileInputStream("src\\mysql.properties"));
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");
        Class.forName(driver);//可以不写(系统也会帮我们完成注册),但是写上更加明确
        Connection connection = DriverManager.getConnection(url,user,password);//通过DriverManager得到链接
        System.out.println("方式5----"+connection);
    }
}
